package com.baigao.springboot.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * Title: 项目名称_[主功能]_[模块名]
 * </p>
 * <p>
 * Description: [文件导出工具类  ]
 * </p>
 * 
 * @author Administrator
 * @version $Revision$ 2019年1月9日
 * @author (lastest modification by $Author$)
 * @since 20180901
 */
public class ExportFileUtils {
	/*
   *//**
	* @描述：Excel导出工具类  获取workBook
	* @param response
	* @param fileName
	* @return   
	* @创建时间：2012-9-24 下午01:30:14   
	* @修改人：   
	* @修改时间：   
	* @修改备注：   
	* @version 1.0
	*//*
	public static WritableWorkbook getExportExcelWorkBook(HttpServletResponse response,String fileName){
		 try {
			OutputStream os = response.getOutputStream();
			response.reset();// 清空输出流   
			String file = fileName+".xls";
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file, "UTF-8"));// 设定输出文件头    
			response.setContentType("application/msexcel");// 定义输出类型
			WritableWorkbook wbook  = Workbook.createWorkbook(os);
			return wbook;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	*//**
	* @描述：导出excel的
	* @param response
	* @param wbook   
	
	* @创建时间：2012-9-24 下午02:01:19   
	* @修改人：   
	* @修改时间：   
	* @修改备注：   
	* @version 1.0
	*
	 *//*
	public static void exportExcelTrigger(HttpServletResponse response,WritableWorkbook wbook){
		try {
			wbook.write(); // 写入文件
			wbook.close();
			response.getOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}*/
	
	/**
	* @描述：导出PDF
	* @param response
	* @param document   
	
	* @创建时间：2012-9-24 下午02:06:33   
	* @修改人：   
	* @修改时间：   
	* @修改备注：   
	* @version 1.0
	*
	 */
	public static void exportPdfTrigger(HttpServletResponse response,String fileName,ByteArrayOutputStream stream){
		try {
			response.reset();// 清空输出流   
			String file = fileName.replaceAll(".pdf", "")+".pdf";
			response.addHeader("Content-Disposition","attachment;filename=" +URLEncoder.encode(file,"UTF-8"));
		    response.setContentType("application/pdf;charset=UTF-8");
		    response.setContentLength(stream.size());
		    ServletOutputStream out = response.getOutputStream();
		    stream.writeTo(out);
		    out.flush();
		}  catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * <p>
	 * Description:[下载模板信息 适用于windows和linux]
	 * </p>
	 * 
	 * Created by [chenkui] [2018年9月19日]
	 * Midified by [修改人] [修改时间]
	 * @param response
	 * @param request
	 * @param path
	 * @param templeteName
	 * @throws IOException
	 */
    public static void downloadExcelTemplate(HttpServletResponse response,HttpServletRequest request,String path,String templeteName) throws IOException {
        OutputStream outp = null;
        FileInputStream in = null;
        try {
            String fileName = templeteName; //要下载的模板文件
            if(templeteName!=null){
                if(!templeteName.endsWith(".xls")){
                    fileName = templeteName + ".xls";
                }
            }
            String filedownload = path + "/" + fileName;
            fileName = URLEncoder.encode(fileName, "UTF-8");
            // 要下载的模板所在的绝对路径
            response.reset();
            response.addHeader("Content-Disposition", "attachment; filename="+fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            outp = response.getOutputStream();
            in = new FileInputStream(filedownload);
            byte[] b = new byte[1024];
            int i = 0;
            while ((i = in.read(b)) > 0) {
                outp.write(b, 0, i);
            }
            outp.flush();
        } catch (Exception e) {
            System.out.println("Error!");
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
                in = null;
            }
            if (outp != null) {
                outp.close();
                outp = null;
            }
        }
    }
    
    /**
     * <p>
     * Description:[方法功能中文描述]
     * </p>
     * 
     * Created by [Administrator] [2019年1月9日]
     * Midified by [修改人] [修改时间]
     * @param response
     * @param request
     * @param path
     * @param fileName 文件名称（含后缀名）
     * @throws IOException
     */
    public static void downloadFile(HttpServletResponse response,HttpServletRequest request,String path,String fileName) throws IOException {
    	OutputStream outp = null;
    	FileInputStream in = null;
    	try {
    		String filedownload = path + "/" + fileName;
    		fileName = URLEncoder.encode(fileName, "UTF-8");
    		// 要下载的模板所在的绝对路径
    		response.reset();
    		response.addHeader("Content-Disposition", "attachment; filename="+fileName);
    		response.setContentType("application/octet-stream;charset=UTF-8");
    		outp = response.getOutputStream();
    		in = new FileInputStream(filedownload);
    		byte[] b = new byte[1024]; //  图片文件流缓存池
    		int i = 0;
    		while ((i = in.read(b)) > 0) {
    			outp.write(b, 0, i);
    		}
    		outp.flush();
    	} catch (Exception e) {
    		System.out.println("Error!");
    		e.printStackTrace();
    	} finally {
    		if (in != null) {
    			in.close();
    			in = null;
    		}
    		if (outp != null) {
    			outp.close();
    			outp = null;
    		}
    	}
    }
}